10/09/2021

Welcome to A Brief Introduction to Programming with R!

Our Team - At Your Service

Thomas Aeschbacher Jan Serwart Ulrich Matter
(TA) (JS) (UM)

The Plan for Today

  • 09:15-10:00 Introduction, Background, Tools (UM [TA, JS])
  • 10:00-10:15 Break, support with installations (TA, JS)
  • 10:15-11:00 Exercises, First Steps with R (TA, JS)
  • 11:00-11:15 Break, Q&A (TA, JS)
  • 11:15-12:00 First Steps with R, Concepts (TA, JS)
  • 12:00-13:15 Lunch (individually)
  • 13:15-14:00 Exercises (TA, JS)
  • 14:00-14:15 Break, Q&A (TA, JS)
  • 14:15-15:00 Working with Data (TA, JS)

Why learn to program (now)?

Relevance for modern economic research

Relevance for modern economic research

Relevance for modern economic research

Relevance for modern economic research

Economy/Business

Perspective: Data Science and Economics

Reasons beyond current developments

  • Programming is an exercise in elementary logic.
  • Better understand the problem at hand.
  • “Programming is like magic!” (Aaron Swartz)

Why R?

The ‘data language’

  • Widely used in Data Science jobs.
  • Particularly useful to program with data.

  • Originally designed as a tool for statistical analysis.

High-level language

  • Relatively easy to learn.
  • A lot free tutorials, cheatsheets, and support online.

Free, open source, large community

  • Used in various fields.
  • Thousands of ‘R-packages’ covering diverse aspects of data analysis.
  • Learn from open sources.
library(ctv)
available.views()
## 
## CRAN Task Views
## ---------------
## Name: Bayesian
## Topic: Bayesian Inference
## Maintainer: Jong Hee Park
## Repository: http://cran.rstudio.com/
## ---------------
## Name: ChemPhys
## Topic: Chemometrics and Computational Physics
## Maintainer: Katharine Mullen
## Repository: http://cran.rstudio.com/
## ---------------
## Name: ClinicalTrials
## Topic: Clinical Trial Design, Monitoring, and Analysis
## Maintainer: H.G. Zhang, Ed Zhang
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Cluster
## Topic: Cluster Analysis & Finite Mixture Models
## Maintainer: Friedrich Leisch, Bettina Gruen
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Databases
## Topic: Databases with R
## Maintainer: Yuan Tang
## Repository: http://cran.rstudio.com/
## ---------------
## Name: DifferentialEquations
## Topic: Differential Equations
## Maintainer: Karline Soetaert, Thomas Petzoldt
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Distributions
## Topic: Probability Distributions
## Maintainer: Christophe Dutang, Patrice Kiener
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Econometrics
## Topic: Econometrics
## Maintainer: Achim Zeileis
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Environmetrics
## Topic: Analysis of Ecological and Environmental Data
## Maintainer: Gavin Simpson
## Repository: http://cran.rstudio.com/
## ---------------
## Name: ExperimentalDesign
## Topic: Design of Experiments (DoE) & Analysis of Experimental Data
## Maintainer: Ulrike Groemping
## Repository: http://cran.rstudio.com/
## ---------------
## Name: ExtremeValue
## Topic: Extreme Value Analysis
## Maintainer: Christophe Dutang, Kevin Jaunatre
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Finance
## Topic: Empirical Finance
## Maintainer: Dirk Eddelbuettel
## Repository: http://cran.rstudio.com/
## ---------------
## Name: FunctionalData
## Topic: Functional Data Analysis
## Maintainer: Fabian Scheipl
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Genetics
## Topic: Statistical Genetics
## Maintainer: Giovanni Montana
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Graphics
## Topic: Graphic Displays & Dynamic Graphics & Graphic Devices & Visualization
## Maintainer: Nicholas Lewin-Koh
## Repository: http://cran.rstudio.com/
## ---------------
## Name: HighPerformanceComputing
## Topic: High-Performance and Parallel Computing with R
## Maintainer: Dirk Eddelbuettel
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Hydrology
## Topic: Hydrological Data and Modeling
## Maintainer: Sam Albers, Sam Zipper,  Ilaria Prosdocimi
## Repository: http://cran.rstudio.com/
## ---------------
## Name: MachineLearning
## Topic: Machine Learning & Statistical Learning
## Maintainer: Torsten Hothorn
## Repository: http://cran.rstudio.com/
## ---------------
## Name: MedicalImaging
## Topic: Medical Image Analysis
## Maintainer: Brandon Whitcher
## Repository: http://cran.rstudio.com/
## ---------------
## Name: MetaAnalysis
## Topic: Meta-Analysis
## Maintainer: Michael Dewey
## Repository: http://cran.rstudio.com/
## ---------------
## Name: MissingData
## Topic: Missing Data
## Maintainer: Julie Josse, Nicholas Tierney, Nathalie Vialaneix (r-miss-tastic team)
## Repository: http://cran.rstudio.com/
## ---------------
## Name: ModelDeployment
## Topic: Model Deployment with R
## Maintainer: Yuan Tang
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Multivariate
## Topic: Multivariate Statistics
## Maintainer: Paul Hewson
## Repository: http://cran.rstudio.com/
## ---------------
## Name: NaturalLanguageProcessing
## Topic: Natural Language Processing
## Maintainer: Fridolin Wild, Performance Augmentation Lab (PAL), Oxford Brookes University, UK
## Repository: http://cran.rstudio.com/
## ---------------
## Name: NumericalMathematics
## Topic: Numerical Mathematics
## Maintainer: Hans W. Borchers
## Repository: http://cran.rstudio.com/
## ---------------
## Name: OfficialStatistics
## Topic: Official Statistics & Survey Methodology
## Maintainer: Matthias Templ
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Optimization
## Topic: Optimization and Mathematical Programming
## Maintainer: Stefan Theussl, 
## Florian Schwendinger, Hans W. Borchers
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Pharmacokinetics
## Topic: Analysis of Pharmacokinetic Data
## Maintainer: Bill Denney
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Phylogenetics
## Topic: Phylogenetics, Especially Comparative Methods
## Maintainer: Brian O'Meara
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Psychometrics
## Topic: Psychometric Models and Methods
## Maintainer: Patrick Mair
## Repository: http://cran.rstudio.com/
## ---------------
## Name: ReproducibleResearch
## Topic: Reproducible Research
## Maintainer: John Blischak, Alison Hill
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Robust
## Topic: Robust Statistical Methods
## Maintainer: Martin Maechler
## Repository: http://cran.rstudio.com/
## ---------------
## Name: SocialSciences
## Topic: Statistics for the Social Sciences
## Maintainer: John Fox
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Spatial
## Topic: Analysis of Spatial Data
## Maintainer: Roger Bivand
## Repository: http://cran.rstudio.com/
## ---------------
## Name: SpatioTemporal
## Topic: Handling and Analyzing Spatio-Temporal Data
## Maintainer: Edzer Pebesma
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Survival
## Topic: Survival Analysis
## Maintainer: Arthur Allignol and Aurelien Latouche
## Repository: http://cran.rstudio.com/
## ---------------
## Name: TeachingStatistics
## Topic: Teaching Statistics
## Maintainer: Paul Northrop
## Repository: http://cran.rstudio.com/
## ---------------
## Name: TimeSeries
## Topic: Time Series Analysis
## Maintainer: Rob J Hyndman
## Repository: http://cran.rstudio.com/
## ---------------
## Name: Tracking
## Topic: Processing and Analysis of Tracking Data
## Maintainer: Rocío Joo, Matthew E. Boone, Michael Sumner and Mathieu Basille
## Repository: http://cran.rstudio.com/
## ---------------
## Name: WebTechnologies
## Topic: Web Technologies and Services
## Maintainer: Scott Chamberlain, Thomas Leeper, Patrick Mair, Karthik Ram, Christopher Gandrud
## Repository: http://cran.rstudio.com/
## ---------------
## Name: gR
## Topic: gRaphical Models in R
## Maintainer: Soren Hojsgaard
## Repository: http://cran.rstudio.com/

The Tools

R

Install R from here!

RStudio

Install RStudio from here!

RStudio Cloud

  • Register here for free!
  • Note: feel free to use this for today’s course if you currently have problems with the local R/RStudio installation.
  • Hint: free exercises that build/extend on what we cover today!

Exercises

Exercise A: Setting up a Working Environment

  1. Open RStudio and get familiar with the file browser pane on the lower right. Navigate to a folder on your hard disk in which you want to work throughout this course (and store all the code you write in this course).

  2. Use the ‘New Folder’-button to create a new folder. Name this new folder r_course.

  3. You should see the new folder listed in the file browser. Click on it to navigate to its contents (so far empty). Now, click on the ‘More’ button and select ‘Set as Working Directory’ in the drop-down menu.

  4. Again, use the ‘New Folder’-button in order to create two new folders called data and code.

  5. Finally, click on the project button in the top-right corner of the RStudio window () and select ‘New Project’ in the drop-down menu. In the pop-up window, select ‘Existing directory’, browse to and select your r_course folder, then click ‘Create Project’.

Now you know how to set up a meaningful basic folder structure and working environment for an R project. The next exercise teaches you how to write R scripts in this environment.

Exercise B: R Scripts

  1. Switch to the R console and type the following line of code and hit enter (see example from above).
print("Hello world")

You should see the words "Hello world" printed on screen. This is the usual way of working with R in an interactive session. However, as pointed out above, in most circumstances it makes sense to write the R code to an R script (in order to store and document it) and then execute the code from there.

  1. In the RStudio Menu-bar select File/New File/R Script to create a new file, shown/opened in the Script pane.

  2. Type print("Hello world") to the first line of the script, and click on ‘Run’ () to execute the code in the console.

Q&A